// create-msg.js
const app = getApp();
const qiniu = require("../../utils/qiniuCdn");
const util = require("../../utils/util");
var content = "";
const max = 9;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgs: [],
    },
    navigateBack() {
        wx.navigateBack({

        })
    },
    onLoad: function (options) {
        this.setData({
            id: options.id,
            imgSize: (app.window.width - 20 - 30) / 4
        });
    },

    onShow() {
        content = ""
    },

    selectImg() {
        wx.chooseImage({
            count: max - this.data.imgs.length,
            success: res => {
                this.setData({
                    imgs: (this.data.imgs || []).concat(res.tempFilePaths),
                })
            },
        })
    },
    dropImg(evt) {
        let imgs = this.data.imgs;
        imgs.splice(evt.currentTarget.dataset.index, 1);
        this.setData({
            imgs
        })
    },
    preImg(evt) {
        let { current } = evt.currentTarget.dataset;
        wx.previewImage({
            urls: this.data.imgs,
            current
        })
    },
    onInput(evt) {
        this.content = evt.detail.value;
    },
 
    submit() {
        wx.showLoading({
            title: '请稍后...',
        });

        qiniu.upload(this.data.imgs, this.data.token).then(keys => {
            console.log(content)

            if (!keys && !this.content) {
                app.wxToast.error("内容或图片不能为空");
                return;
            }
            app.http.post("shop-circle/create", {
                data: {
                    content: this.content,
                    images: keys,
                }
            }).then(res => {
                res.data.created_at = util.formatTime(new Date(res.data.created_at * 1000))
                app.newMsg = res.data
                wx.hideLoading();
                wx.navigateBack();
            }).catch(err => {
                wx.hideLoading();
                app.wxToast.error(err.message);
            })
        }).catch(err => {
            console.log(err);
            wx.hideLoading();
            app.wxToast.error("图片上传失败");
        })

    },

    upload() {

    }
})